package com.siblings.loan.ui.loan.fm

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.siblings.loan.R
import com.siblings.loan.databinding.FmLoanOrderBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.loan.adapter.SibLoanOrderAdapter
import com.siblings.loan.ui.loan.impl.SibLoanOrderPresenterImpl
import com.siblings.loan.ui.loan.view.ISibLoanOrderView
import com.siblings.loan.ui.tab.act.SibMainAct
import com.siblings.loan.ui.user.act.SibMessageAct
import com.siblings.loan.utils.event.SibEventData
import com.siblings.loan.utils.event.SibEventType
import com.siblings.mvp.MvpFm
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/8 18:12
 * @Annotation：贷款还款
 */
class SibLoanOrderFm: MvpFm<ISibLoanOrderView,SibLoanOrderPresenterImpl>(),ISibLoanOrderView {
    lateinit var binding: FmLoanOrderBinding
    //订单列表
    private var orderAdapter: SibLoanOrderAdapter? = null
    override fun getSibRootView(inflater: LayoutInflater): View {
        binding = FmLoanOrderBinding.inflate(inflater)
        return binding.root
    }

    override fun createMvpPresenter(): SibLoanOrderPresenterImpl {
        return SibLoanOrderPresenterImpl(context)
    }

    override fun initSibView() {
        setSibTitleBarVisibility(View.VISIBLE)
        setSibTitleBarColor(Color.parseColor("#FFF9F9F9"))
        setSibTextTitle("")
        setSibBackBtnRes(R.mipmap.pic_title_menu_theme_icon)
        setSibRightBtnRes(R.mipmap.pic_title_message_theme_icon)
        //拦截返回
        isInterceptBack(true)

        initSibRefreshLayout()
        initSibOrderRecyclerView()
        EventBus.getDefault().register(this)
    }

    override fun initSibListener() {
        //菜单
        rootViewBinding.ivBaseBack.setOnClickListener {
            val tabAct = context as SibMainAct
            tabAct.showSibMenuFragment("OrderList Page")
        }

        //消息
        rootViewBinding.ivFun.setOnClickListener {
            onIntent(SibMessageAct::class.java)
        }
    }

    override fun initSibData() {
        showSibLoading()
        mvpPresenter.loadSibLoanOrderListAction()
    }

    /* 初始化刷新 */
    private fun initSibRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            mvpPresenter.loadSibLoanOrderListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 订单列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initSibOrderRecyclerView(){
//        binding.recyclerView.setHasFixedSize(true)
//        binding.recyclerView.isNestedScrollingEnabled = false
//        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
//        val decoration = DensityUtils.dp2px(9f)
//        binding.recyclerView.addItemDecoration(GridSpacingItemDecoration(3,decoration,false))
        val layoutManager = LinearLayoutManager(getContext())
        binding.recyclerView.layoutManager = layoutManager
        orderAdapter = SibLoanOrderAdapter(binding.recyclerView)
        binding.recyclerView.adapter = orderAdapter
        orderAdapter!!.setOnItemChildClickListener { _, childView, position ->
            val item = this.orderAdapter!!.getItem(position)
            if (childView.id == R.id.btnRepay){
                mvpPresenter.onSibOrderPayLinkAction(item.optString("orderId"))
            }
        }
    }

    /**
     * 刷新数据
     */
    fun onSibRefreshOrderList(){
        mvpPresenter.loadSibLoanOrderListAction()
    }

    override fun showSibEmptyView() {
        binding.emptyLayout.visibility = View.VISIBLE
        binding.recyclerView.visibility = View.GONE
    }

    override fun showSibOrderList(orderList: ArrayList<JSONObject>) {
        binding.emptyLayout.visibility = View.GONE
        binding.recyclerView.visibility = View.VISIBLE
        orderAdapter!!.data = orderList
    }

    /**
     * 还款链接
     */
    override fun showSibOrderPayLink(payLink: String) {
        val uri = Uri.parse(payLink)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)
    }

    override fun hideSibLoading() {
        super.hideSibLoading()
        binding.refreshLayout.finishRefresh()
    }

    /**
     * 更新总流程完成
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public fun onRefreshProcessCompleted(event: SibEventData){
        if (event.eventType == SibEventData.TYPE.SIB_PROCESS_REFRESHED){
            mvpPresenter.loadSibLoanOrderListAction()
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_ORDER_LIST_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_ORDER_LIST_END,"")
        EventBus.getDefault().unregister(this)
    }
}